package com.mikel.monitor.misc;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mikel.monitor.domain.ZbNetworkState;
import com.mikel.monitor.services.ZbNetworkStateService;
import com.mikel.monitor.misc.TwoWaySerialComm;


@Component
public class ZbNetworkStateJob {

	@Autowired
	private ZbNetworkStateService zbNetworkStateService;

	public void execute() {
		System.out.println("chequeando estado");

		while (true) {
			List<ZbNetworkState> zbNetworkStateList = zbNetworkStateService
					.findAllZbNetworkStates();
			System.out.println("encontrados " + zbNetworkStateList.size());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		

	}
	
	public void execute3() {
		System.out.println("chequeando estado");
		
		while (true) {
			
			creaEventosAleatorios();
			creaRegistrosAleatorios();

		}		
		
	}
	
	public void creaEventosAleatorios() {
		//eventos
		Date date = new Date();
		
		ZbNetworkState zbNetworkState = new ZbNetworkState();
		zbNetworkState.setNombreParametro("Interruptor magnético");
		zbNetworkState.setValor("evento");
		zbNetworkState.setFecha(date);
		zbNetworkState.setNodo("3");
		
		zbNetworkState.persist();
		
		System.out.println("Registro creado: " + "evento" + ", a las "+date );
		
		int max = 10000;
		int min = 0;
		Random rand = new Random();
		int r = rand.nextInt((max - min) + 1) + min;
		try {
			Thread.sleep(r);
							
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void creaRegistrosAleatorios() {

		// temperatura
		Date date = new Date();

		ZbNetworkState zbNetworkState = new ZbNetworkState();
		zbNetworkState.setNombreParametro("Temperatura");
		int max = 250;
		int min = 200;
		Random rand = new Random();
		float temperatura = (rand.nextInt((max - min) + 1) + min) / 10f;
		zbNetworkState.setValor(String.valueOf(temperatura) + " ºC");
		zbNetworkState.setFecha(date);
		zbNetworkState.setNodo("4");

		zbNetworkState.persist();
		System.out.println("Registro creado: " + temperatura + ", a las "
				+ date);

		// consumo
		min = 1;
		max = 120;
		int consumo = rand.nextInt((max - min) + 1) + min;
		date = new Date();
		zbNetworkState = new ZbNetworkState();
		zbNetworkState.setNombreParametro("Consumo eléctrico");
		zbNetworkState.setValor(String.valueOf(consumo) + " W");
		zbNetworkState.setFecha(date);
		zbNetworkState.setNodo("1");

		zbNetworkState.persist();
		System.out.println("Registro creado: " + consumo + ", a las " + date);

		// luminosidad
		min = 1;
		max = 10;
		int lightInt = rand.nextInt((max - min) + 1) + min;
		String luminosidad;
		if (lightInt > 6)
			luminosidad = "Alta";
		else if (lightInt > 3)
			luminosidad = "Media";
		else
			luminosidad = "Baja";

		date = new Date();

		zbNetworkState = new ZbNetworkState();
		zbNetworkState.setNombreParametro("Luminosidad");
		zbNetworkState.setValor(luminosidad);
		zbNetworkState.setFecha(date);
		zbNetworkState.setNodo("2");

		zbNetworkState.persist();
		System.out.println("Registro creado: " + luminosidad + ", a las "
				+ date);

		try {
			Thread.sleep(10000);

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
    public static void execute2()
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM13");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
